package com.chaokang.controller;

import com.chaokang.Entity.CommonUserEntity;
import com.chaokang.Entity.GroupEntity;
import com.chaokang.Entity.GroupMemberEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
public class GroupConsumerController {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${service-url.nacos-user-service}")
    private String commonUserUrl;
    public HashMap getGroupMemberHash(GroupMemberEntity groupMemberEntity){
        HashMap<String,Object> map = new HashMap<>();
        if(groupMemberEntity.getId()!=null){
            map.put("id",groupMemberEntity.getId());
        }
        if(groupMemberEntity.getGroupId()!=null){
            map.put("groupId",groupMemberEntity.getGroupId());
        }
        if(groupMemberEntity.getUserId()!=null){
            map.put("userId",groupMemberEntity.getUserId());
        }
        if(groupMemberEntity.getMemberId()!=null){
            map.put("memberId",groupMemberEntity.getMemberId());

        }
        return map;
    }
    public HashMap getGroupHash(GroupEntity groupEntity){
        HashMap<String,Object> map = new HashMap<>();
        if(groupEntity.getId()!=null){
            map.put("id",groupEntity.getId());
        }
        if(groupEntity.getName()!=null){
            map.put("name",groupEntity.getName());
        }
        if(groupEntity.getUserId()!=null){
            map.put("userId",groupEntity.getUserId());
        }
        if(groupEntity.getNumber()!=null){
            map.put("number",groupEntity.getNumber());
        }
        return map;
    }
    @PostMapping("/insertGroupMember")
    public HashMap<String,Integer> insertGroupMember(@RequestBody GroupMemberEntity groupMemberEntity){
        HashMap map = getGroupMemberHash(groupMemberEntity);
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        HashMap a=restTemplate.postForObject(commonUserUrl + "/service/insertGroupMember", objectHttpEntity, HashMap.class);



        return map;
    }
    @PostMapping("/getGroupByUserId")
    public List<GroupEntity> getGroupByUserId(@RequestBody GroupEntity groupEntity){
        HashMap map = getGroupHash(groupEntity);
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        List<GroupEntity> a=restTemplate.postForObject(commonUserUrl + "/service/getGroupByUserId", objectHttpEntity, List.class);
        return a;

    }
    @PostMapping("/getGroupMemberByGroupId")
    public List<CommonUserEntity> getGroupMemberByGroupId(@RequestBody GroupMemberEntity groupMemberEntity){

        HashMap map = getGroupMemberHash(groupMemberEntity);
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        List<CommonUserEntity> a=restTemplate.postForObject(commonUserUrl + "/service/getGroupMemberByGroupId", objectHttpEntity, List.class);
        return a;
    }
    @PostMapping("/deleteGroupMember")
    public HashMap deleteGroupMember(@RequestBody GroupMemberEntity groupMemberEntity){
        HashMap<String,Object> map = getGroupMemberHash(groupMemberEntity);
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        HashMap a=restTemplate.postForObject(commonUserUrl + "/service/deleteGroupMember", objectHttpEntity, HashMap.class);

        return a;
    }
    @PostMapping("/addNewGroup")
    public HashMap addNewGroup(@RequestBody  GroupEntity groupEntity){
        HashMap<String,Object> map = getGroupHash(groupEntity);
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        HashMap a=restTemplate.postForObject(commonUserUrl + "/service/addNewGroup", objectHttpEntity, HashMap.class);

        return a;
    }
}
